package com.hnichr.ump.assessment.controller;


import com.hnichr.ump.common.assessment.entity.Invite;
import com.hnichr.ump.common.assessment.entity.Testee;
import com.hnichr.ump.common.assessment.service.InviteService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/assessment/invite/v1/")
@Tag(name = "InviteController", description = "受测邀请信息接口")
public class InviteController {

    private static Logger logger = LoggerFactory.getLogger(InviteController.class);

    @Autowired
    private InviteService inviteService;

    @GetMapping("list")
    @Operation(summary = "受测邀请信息列表", description = "获取受测邀请信息列表（无分页）", method = "GET")
    public Result<List<Invite>> list(@Parameter(description="查询参数") Criteria criteria){
        List<Invite> list = inviteService.list(criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "受测邀请信息列表", description = "受测邀请信息列表（分页）", method = "GET")
    public Result<Page<Invite>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<Invite> list = inviteService.listPage(page, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取受测邀请信息", description = "根据ID获取受测邀请信息", method = "GET")
    public Result<Invite> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        Invite activity = inviteService.getById(Invite.class, id);
        return Result.OK(activity);
    }

    @PostMapping("add")
    @Operation(summary = "新增受测邀请信息", description = "新增受测邀请信息", method = "POST")
    public Result<Testee> add(@RequestBody Testee testee) {
        return Result.error("受测邀请信息不允许新增");
    }

    @PutMapping("edit")
    @Operation(summary = "编辑受测邀请信息", description = "编辑受测邀请信息", method = "PUT")
    public Result<Testee> edit(@RequestBody Testee testee) {
        return Result.error("受测邀请信息不允许编辑");
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除受测邀请信息", description = "根据id删除受测邀请信息", method = "DELETE")
    public Result<Testee> remove(@PathVariable(name = "ids", required = true) String ids) {
        return Result.error("受测邀请信息不允许本地直接删除");
    }

    @GetMapping("download/{id}")
    public void download(@PathVariable(name = "id", required = true) String id, HttpServletResponse response){
        response.setContentType("application/octet-stream");
        // 设置在下载框默认显示的文件名
        try {
            ServletOutputStream out = response.getOutputStream();
            inviteService.download(id,out);
            out.flush();
        }
        catch (Exception e){
            logger.error("下载失败!",e);
        }
    }
}
